<?php
header('Content-Type: application/json');

$ip = get_client_ip();
$seat = get_client_seat($ip);
$data = get_client_data($seat);
$controllers = get_client_info($data);

echo json_encode(array(
  'ip' => $ip,
  'seat' => $seat,
  'clients' => $clients,
), JSON_UNESCAPED_UNICODE);

function get_client_ip() {
  $ip = null;
  if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
    $ip = trim(current($ip));
  }
  return $ip;
}

function get_client_seat($ip) {
  $client_list = [
    ['seat' => 'TM05', 'ip' => ['10.202.91.20', '10.202.91.120']],
    ['seat' => 'TM06', 'ip' => ['10.202.91.21', '10.202.91.121']],
    ['seat' => 'TM12', 'ip' => ['10.202.91.22', '10.202.91.122']],
    ['seat' => 'TM07', 'ip' => ['10.202.91.23', '10.202.91.123']],
    ['seat' => 'TM09', 'ip' => ['10.202.91.24', '10.202.91.124']],
    ['seat' => 'TM03', 'ip' => ['10.202.91.25', '10.202.91.125']],
    ['seat' => 'TM04', 'ip' => ['10.202.91.26', '10.202.91.126']],
    ['seat' => 'TM11', 'ip' => ['10.202.91.7', '10.202.91.107']],
    ['seat' => 'TM02', 'ip' => ['10.202.91.6', '10.202.91.106']],
    ['seat' => 'TM01', 'ip' => ['10.202.91.5', '10.202.91.105']],
    ['seat' => 'TM08', 'ip' => ['10.202.91.4', '10.202.91.104']],
    ['seat' => 'TM10', 'ip' => ['10.202.91.3', '10.202.91.103']],
    ['seat' => 'TM13', 'ip' => ['10.202.91.2', '10.202.91.102']],
    ['seat' => 'TM14', 'ip' => ['10.202.91.1', '10.202.91.101']],
  ];
  foreach ($client_list as $client) {
    if ($client['ip'][0] == $ip || $client['ip'][1] == $ip) {
      return $client['seat'];
    }
  }
  return '其他';
}

function get_client_data($seat) {
  if ($seat == '其他') {
    return '';
  }

  // 定义Cookie存储路径
  $cookie_jar = dirname(__FILE__) . "/seat_atcos_cookie.txt";

  // 将cookie存入文件
  // $url = "http://172.22.107.22/atcos/loginGet?userName={$seat}&pwd={$seat}";
  $url = "http://172.22.107.22/atcos/loginGet?userName=PREP&pwd=PREP";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  // curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
  $content = curl_exec($ch);
  curl_close($ch);

  // 到指定页面获取数据
  $ch = curl_init();
  $url = "http://172.22.107.22/atcos/workingHours/getAllNotEndWorkingHours?seatCode={$seat}";
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_HEADER, false);
  // curl_setopt($ch, CURLOPT_HEADER, 0);
  // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
  $html = curl_exec($ch);
  // var_dump($html);
  curl_close($ch);
  return $html;
}

function get_client_info($html) {
  if ($html == '') {
    return [];
  }

  $res = json_decode($html, true);
  $controller_a = '';
  $controller_b = '';
  $controller_c = '';
  $controller_as = '';
  $controller_bs = '';
  $controller_cs = '';
  $count = 0;
  foreach ($res as $user) {
    if ($user['seatRole']['name'] == '管制' || $user['seatRole']['name'] == '教员') {
      if ($user['seatPost']['name'] == '管制岗') {
        $controller_a = array('userId' => $user['userId'], 'userName' => $user['userName']);
        $count++;
      } elseif ($user['seatPost']['name'] == '协调岗') {
        $controller_b = $user['userName'];
        $count++;
      } elseif ($user['seatPost']['name'] == '第三岗') {
        $controller_c = $user['userName'];
        $count++;
      }
    } else {
      if ($user['seatPost']['name'] == '管制岗') {
        $controller_as = $user['userName'];
        $count++;
      } elseif ($user['seatPost']['name'] == '协调岗') {
        $controller_bs = $user['userName'];
        $count++;
      } elseif ($user['seatPost']['name'] == '第三岗') {
        $controller_cs = $user['userName'];
        $count++;
      }
    }
  }
  return array(
    'count' => $count,
    'conA' => $controller_a,
    'conB' => $controller_b,
    'conC' => $controller_c,
    'conAS' => $controller_as,
    'conBS' => $controller_bs,
    'conCS' => $controller_cs,
  );
}